//
//  DCTabBadgeView.m
//  商城
//
//  Created by runone02 on 2018/8/27.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "DCTabBadgeView.h"

@implementation DCTabBadgeView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpBase];
    }
    return self;
}

- (void)setUpBase{
    self.userInteractionEnabled = NO;
    self.titleLabel.font = TITLE_FONT(22);
    self.titleLabel.textAlignment = NSTextAlignmentRight;
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.backgroundColor = RGB(226, 70, 157);
    
    WeakSelf(weakSelf);
    [[NSNotificationCenter defaultCenter]addObserverForName:@"jump" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
        animation.keyPath = @"transform.scale";
        animation.values = @[@1.0,@1.1,@0.9,@1.0];
        animation.duration = 0.3;
        animation.calculationMode = kCAAnimationCubic;
        //添加动画
        [weakSelf.layer addAnimation:animation forKey:nil];
        
    }];
}

- (void)setBadgeValue:(NSString *)badgeValue{
    _badgeValue = badgeValue;
    
}

- (void)setBadgeViewWithbadgeValue:(NSString *)badgeValue{
    //设置文字内容
    [self setTitle:badgeValue forState:UIControlStateNormal];
    
    //判断内容是否为空,设置隐藏属性
    self.hidden = (badgeValue.length == 0 || [badgeValue isEqualToString:@"0"])?  YES : NO;
    NSInteger badgrNumber = [badgeValue integerValue];
    
    //如果文字尺寸大于控件宽度
    if (badgrNumber > 99) {
        [self setTitle:@"99+" forState:UIControlStateNormal];
    }
    
    self.dc_size = CGSizeMake(22, 22);
    [DCSpeedy dc_chageControlCircularWith:self AndSetCornerRadius:11 SetBorderWidth:1 SetBorderColor:RGB(245,245,245) canMasksToBounds:YES];
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
